﻿using System;
using System.Collections.Generic;
using eShop.Business;
using eShop.DataAccess;
using eShop.Model;
using eShop.Utility;

namespace eShop.Business
{
    public class ManufacturerManager
    {
        #region _ACTIVE_FUNCTION

        public static void CreateManufacturer(Manufacturer manufacturer)
        {
            if (IsBrandNameUnique(manufacturer.BrandName) == false)
            {
                throw new InvalidOperationException("There is already a manufacturer with same name.");
            }
            manufacturer.BrandUrl = CreateManufacturerUrl(manufacturer);
            if (!String.IsNullOrEmpty(manufacturer.BrandImageUrl))
            {
                manufacturer.BrandImageUrl = WebPresentationUtility.CopyImageAndResize(manufacturer.BrandImageUrl,
                    ApplicationSettings.ManufacturerImagePath, string.Empty, ApplicationSettings.BrandLogoImageWidth);
            }
            if (!String.IsNullOrEmpty(manufacturer.BrandBannerImageUrl))
            {
                manufacturer.BrandBannerImageUrl = WebPresentationUtility.CopyImageAndResize(manufacturer.BrandBannerImageUrl,
                    ApplicationSettings.ManufacturerImagePath, string.Empty, ApplicationSettings.BrandBannerImageWidth);
            }
            manufacturer.ProductBrandId = ManufacturerDAO.CreateNewTblProductBrand(manufacturer);
        }

        public static void UpdateManufacturer(Manufacturer manufacturer)
        {
            Manufacturer oldmanufacturer = GetManufacturerById(manufacturer.ProductBrandId);
            if (string.Compare(oldmanufacturer.BrandName, manufacturer.BrandName, true) != 0)
            {
                if (IsBrandNameUnique(manufacturer.BrandName) == false)
                {
                    throw new InvalidOperationException("There is already a brand with same name");
                }
                manufacturer.BrandUrl = CreateManufacturerUrl(manufacturer);
            }
            if (!String.IsNullOrEmpty(manufacturer.BrandImageUrl)
                && string.Compare(oldmanufacturer.BrandImageUrl, manufacturer.BrandImageUrl, true) != 0)
            {
                manufacturer.BrandImageUrl = WebPresentationUtility.CopyImageAndResize(manufacturer.BrandImageUrl,
                    ApplicationSettings.ManufacturerImagePath, string.Empty, ApplicationSettings.BrandLogoImageWidth);
                FileAccessUtility.DeleteFile(oldmanufacturer.BrandImageUrl);
            }
            if (!String.IsNullOrEmpty(manufacturer.BrandBannerImageUrl)
                && string.Compare(oldmanufacturer.BrandBannerImageUrl, manufacturer.BrandBannerImageUrl, true) != 0)
            {
                manufacturer.BrandBannerImageUrl = WebPresentationUtility.CopyImageAndResize(manufacturer.BrandBannerImageUrl,
                    ApplicationSettings.ManufacturerImagePath, string.Empty, ApplicationSettings.BrandBannerImageWidth);
                FileAccessUtility.DeleteFile(oldmanufacturer.BrandBannerImageUrl);
            }
            ManufacturerDAO.UpdateTblProductBrand(manufacturer);
        }

        public static Manufacturer GetManufacturerById(int id)
        {
            if (id > 0)
            {
                return ManufacturerDAO.GetTblProductBrandByProductBrandId(id);
            }
            else
            {
                return null;
            }
        }

        /****************************************/

        private static bool IsBrandNameUnique(string brandName)
        {
            int count = ManufacturerDAO.GetProductBrandCountByName(brandName);
            return count == 0;
        }

        private static string CreateManufacturerUrl(Manufacturer manufacturer)
        {
            return SiteUtility.CleanBrandName(manufacturer.BrandName);
        }

        #endregion


        /****************************************/
        /****************************************/
        /****************************************/


        #region _INACTIVE_FUNCTION

        public static int GetActiveBrandProductsCount(int productBrandId)
        {
            return ProductManager.ProductCountByManufacturer(productBrandId, DataState.Active);
        }


        public static void DeleteBrands(List<int> idList)
        {
            ManufacturerDAO.DeleteTblProductBrandList(idList);
        }

        public static List<Manufacturer> GetBackendBrandList(string orderBy, int startIndex, int pageSize)
        {
            return ManufacturerDAO.GetAllTblProductBrandPaged(orderBy, startIndex, pageSize);
        }

        public static int GetBackendBrandListCount()
        {
            return ManufacturerDAO.GetAllTblProductBrandPagedCount(string.Empty, 0, 0);
        }

        /**************************************************/

        public static Manufacturer GetByBrandUrl(string brandUrl)
        {
            return ManufacturerDAO.GetTblProductBrandByProductBrandUrl(brandUrl);
        }

        public static List<Manufacturer> GetByProductCategory(int categoryId)
        {
            return ManufacturerDAO.GetProductBrandsByPertnerCategory(categoryId);
        }

        public static List<Manufacturer> GetAllManufacturers()
        {
            return ManufacturerDAO.GetAllTblProductBrand();
        }

        public static List<Product> GetActiveBrandProductsPaged(int productBrandId, string displayOrder, int currentPageIndex, int pageSize)
        {
            List<Product> products = new List<Product>();
            products = ProductManager.ProductByManufacturer(productBrandId, currentPageIndex, pageSize, DataState.Active);
            return products;
        }

        #endregion
    }
}